//
//  AssistantsView.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import SwiftUI

struct AssistantsView: View {
    @StateObject var viewModel = AssistantsViewModel()
    @Binding var section: ContentView.Tab
    @Binding var item: MsgItem
    @State private var showDrawImageView = false
    @State private var showRecognizeImagesView = false
    @State private var showSummarizeDocumentView = false
    @State private var showAnalyzeLinkView = false
    @State private var showTranslateView = false
    @State private var showEmojiView = false
    var body: some View {
        GeometryReader(content: { geometry in
            VStack {
                HStack {
                    Image("icon_setting")
                        .resizable()
                        .frame(width: 24, height: 24)
                        .opacity(0)
                    Spacer()
                    Text(String.i18nText("tab_assistants"))
                        .font(.system(size: 20, weight: .bold))
                        .foregroundColor(.white)
                    Spacer()
                    Button(action: {
                        viewModel.logSettingsClick()
                        viewModel.showSettingView.toggle()
                    }, label: {
                        Image("icon_setting")
                            .resizable()
                            .frame(width: 24, height: 24)
//                            .onTapGesture {
//                                viewModel.showSettingView.toggle()
//                            }
                    })
                }
                .padding(.horizontal)
                .padding(.vertical, 8)
                ScrollView {
                    VStack {
                        if !viewModel.isVip {
                            Button(action: {
                                viewModel.logfrom = "assistant_freeassistant"
                                viewModel.logFreeClick()
                                viewModel.showVipView.toggle()
                            }, label: {
                                VipBarView()
                                    .frame(height: 84)
//                                    .onTapGesture {
//                                        viewModel.showVipView.toggle()
//                                    }
                            })
                            Spacer()
                                .frame(height: 22)
                        }

                        FeaturesView(tap: { _, i in
                            if(i == 0){
                                viewModel.logDrawClick()
                                showDrawImageView.toggle()
                                
                            }else if(i == 1){
                                viewModel.logImagesClick()
                                showRecognizeImagesView.toggle()
                            }else if(i == 2){
                                viewModel.logDocumentClick()
                                showSummarizeDocumentView.toggle()
                            }else if(i == 3){
                                viewModel.logLinkClick()
                                showAnalyzeLinkView.toggle()
                            }

                        }).frame(width: geometry.size.width - 40, height: 172)
                            .sheet(isPresented: $showDrawImageView) {
                                DrawImaginationView().presentationDetents([.height(492)])
                            }.sheet(isPresented: $showRecognizeImagesView) {
                                RecognizeImagesView().presentationDetents([.height(566)])
                            }.sheet(isPresented: $showSummarizeDocumentView) {
                                DocumentView().presentationDetents([.height(566)])
                            }.sheet(isPresented: $showAnalyzeLinkView) {
                                LinkView().presentationDetents([.height(566)])
                            }
                        Spacer().frame(height: 24)
                        PopularView(tap: { _, i in
                            
                            if(i == 0){
                                viewModel.logTranslatorClick()
                                showTranslateView.toggle()
                            }else{
                                viewModel.logEmojiClick()
                                showEmojiView.toggle()
                            }

                        })
                            .frame(width: geometry.size.width - 40, height: 105).sheet(isPresented: $showTranslateView) {
                                TranslateView().presentationDetents([.height(566)])
                            }.sheet(isPresented: $showEmojiView) {
                                EmojiView().presentationDetents([.height(566)])
                            }
                        Spacer()
                            .frame(height: 24)
                        WriteAssistansView(tap: { msg, index in
                            if !viewModel.isVip, viewModel.leftCount == 0 {
                                viewModel.logfrom = "assistant_send"
                                viewModel.showVipView.toggle()
                                return
                            }
                            viewModel.logContent(bigId: 1, smallId: index+1)
                            section = .second
                            item = msg
                            NotificationCenter.default.post(name: Notification.Name("kRefreshMSg"), object: item)
                        })
                            .frame(width: geometry.size.width - 40, height: 140)
                        Spacer()
                            .frame(height: 24)
                        EntertainmentView(tap: { msg, index in
                            if !viewModel.isVip, viewModel.leftCount == 0 {
                                viewModel.logfrom = "assistant_send"
                                viewModel.showVipView.toggle()
                                return
                            }
                            viewModel.logContent(bigId: 2, smallId: index+1)

                            section = .second
                            item = msg
                            NotificationCenter.default.post(name: Notification.Name("kRefreshMSg"), object: nil)
                        })
                            .frame(width: geometry.size.width - 40)
                        Spacer()
                            .frame(height: 24)
                        QuickResponseView(tap: { msg, index in
                            if !viewModel.isVip, viewModel.leftCount == 0 {
                                viewModel.logfrom = "assistant_send"
                                viewModel.showVipView.toggle()
                                return
                            }
                            viewModel.logContent(bigId: 3, smallId: index+1)

                            section = .second
                            item = msg
                            NotificationCenter.default.post(name: Notification.Name("kRefreshMSg"), object: item)
                        })
                            .frame(width: geometry.size.width - 40)
                    }
                    .padding()
                }
                HStack(alignment: .center) {
                    Button(action: {
                        section = .second
                    }, label: {
                        VStack(spacing: 0) {
                            Image("ic_mofabang")
                                .resizable()
                                .frame(width: 25, height: 25)
                        }
                    })

                    TextField(String.i18nText("chat_ask"), text: .constant(""))
                        .disabled(true)
                        .background(Color(red: 15/255.0, green: 15/255.0, blue: 17/255.0))
                        .font(.system(size: 14))
                        .textFieldStyle(CustomTextFieldStyle(hasMsg: .constant(false)))
                        .cornerRadius(12)
                        .padding(.vertical, 0)
                        .onTapGesture {
                            section = .second
                        }

                    Button(action: {
                        section = .second
                        viewModel.logChatClick()
                    }, label: {
                        Image("icon_chat_bottom_send_gray")
                            .resizable()
                            .frame(width: 24, height: 24)
                    })
                }
                .padding([.top, .bottom], 0)
                .padding([.trailing, .leading], 16)
                Spacer()
                    .background(Color(red: 15/255.0, green: 15/255.0, blue: 17/255.0))
                    .frame(height: 8)
            }
            .onReceive(NotificationCenter.default.publisher(for: Notification.Name("kAppendMessage"))) { notification in
                // 在这里处理通知
                DispatchQueue.main.async {
                    section = .second
                }
                
            }
            .onReceive(NotificationCenter.default.publisher(for: Notification.Name("kGpt3OverSizeMessage"))) { notification in
                // 在这里处理通知
                DispatchQueue.main.async {
                    section = .second
                }
                
            }
            .onReceive(NotificationCenter.default.publisher(for: Notification.Name("kGpt4OverSizeMessage"))) { notification in
                // 在这里处理通知
                DispatchQueue.main.async {
                    section = .second
                }
                
            }
            .fullScreenCover(isPresented: $viewModel.showVipView, content: {
                VipView(viewModel: .init(from: viewModel.logfrom))
            })
            .fullScreenCover(isPresented: $viewModel.showSettingView, content: {
                SettingView()
            })
            .onAppear {
                viewModel.refreshUI()
            }
        })
    }
}

#Preview {
    AssistantsView(section: .constant(.first), item: .constant(.init(text: "")))
}
